package com.example.a_java.service.impl;

import com.example.a_java.dto.CategoryQuery;
import com.example.a_java.entity.ProductCategory;
import com.example.a_java.mapper.ProductCategoryMapper;
import com.example.a_java.service.ProductCategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 产品分类服务实现
 */
@Service
@RequiredArgsConstructor
public class ProductCategoryServiceImpl implements ProductCategoryService {

    private final ProductCategoryMapper productCategoryMapper;

    @Override
    public List<ProductCategory> getCategoryList(CategoryQuery query) {
        // 查询分类列表，以树形结构返回
        return productCategoryMapper.selectCategoryList(query);
    }

    @Override
    public ProductCategory getCategoryById(Long id) {
        return productCategoryMapper.selectCategoryById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long addCategory(ProductCategory category) {
        // 设置创建时间和更新时�?
        LocalDateTime now = LocalDateTime.now();
        category.setCreateTime(now);
        category.setUpdateTime(now);
        
        // 设置默认状态，确保status字段只有"0"�?1"
        if (category.getStatus() == null) {
            category.setStatus("1");
        } else {
            // 确保status字段值为"0"�?1"
            String status = category.getStatus().trim();
            if ("0".equals(status) || "1".equals(status)) {
                category.setStatus(status);
            } else {
                category.setStatus("1"); // 如果是其他值，默认为启用状�?
            }
        }
        
        // 设置默认编码，如果为空则生成默认�?"CAT" + 当前时间毫秒�?
        if (category.getCode() == null || category.getCode().trim().isEmpty()) {
            category.setCode("CAT" + System.currentTimeMillis());
        }
        
        // 插入分类
        productCategoryMapper.insertCategory(category);
        return category.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCategory(ProductCategory category) {
        // 设置更新时间
        category.setUpdateTime(LocalDateTime.now());
        // 更新分类
        productCategoryMapper.updateCategory(category);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteCategory(Long id) {
        // 检查是否有子分�?
        int childCount = productCategoryMapper.countChildCategories(id);
        if (childCount > 0) {
            throw new RuntimeException("存在子分类，不能删除");
        }
        // 检查是否有关联产品
        int productCount = productCategoryMapper.countCategoryProducts(id);
        if (productCount > 0) {
            throw new RuntimeException("存在关联产品，不能删除");
        }
        // 删除分类
        productCategoryMapper.deleteCategory(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCategoryStatus(ProductCategory category) {
        // 设置更新时间
        category.setUpdateTime(LocalDateTime.now());
        // 更新分类状�?
        productCategoryMapper.updateCategoryStatus(category);
    }
} 
